﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class Npc : Entity, IInteractable, IScriptedPrefab, IEditable
{
    protected Rect dialogRect;
    private static GUISkin guiSkin;

    protected override void Awake()
    {
        base.Awake();
        base.SetLayer(GameLayers.Obstacle);
        base.dontHideAgainIfObserved = true;
        base.model.GenerateCollider();
        if (guiSkin == null)
        {
            guiSkin = UnityEngine.Object.Instantiate<GUISkin>(GameResources.GUISkin);
            guiSkin.label.fontSize = 0x15;
            guiSkin.button.fontSize = 0x15;
        }
    }

    protected void CallServerFunction(string functionName, object parameter = null)
    {
        PlayerCharacter target = this.target as PlayerCharacter;
        if (target != null)
        {
            target.CallServerNpcFunction(this, functionName, parameter);
        }
    }

    public virtual bool CanInteract(Character target)
    {
        if (target == null)
        {
            return false;
        }
        Life life = target.life;
        if ((life != null) && life.isDied)
        {
            return false;
        }
        return true;
    }

    public void EndInteract(Character target)
    {
        if (this.target == target)
        {
            this.OnEndInteract();
            this.target = null;
        }
    }

    void IEditable.OnEditorGUI()
    {
        this.OnEditorGUI();
    }

    public void Interact(Character target)
    {
        if (this.target == null)
        {
            this.target = target;
            this.OnInteract();
        }
    }

    void IScriptedPrefab.InitPrefab()
    {
        base.tag = GameTags.Npc;
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    protected virtual void OnDialogGUI()
    {
    }

    protected virtual void OnEditorGUI()
    {
    }

    protected virtual void OnEndInteract()
    {
    }

    protected virtual void OnGUI()
    {
        if (this.target != null)
        {
            GUI.skin = guiSkin;
            Vector3 position = base.transform.position + ((Vector3) (Vector3.up * 2f));
            Vector3 vector2 = Camera.main.WorldToScreenPoint(position);
            vector2.y = Screen.height - vector2.y;
            this.dialogRect.position = (Vector2) (vector2 - (Vector3.up * this.dialogRect.size.y));
            this.dialogRect.x -= this.dialogRect.width / 2f;
            this.dialogRect.height = 20f;
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.MinWidth(250f) };
            this.dialogRect = GUILayout.Window(this.GetHashCode(), this.dialogRect, delegate (int id) {
                if (!this.target.isLocalPlayer)
                {
                    GUIStyle style = new GUIStyle(GUI.skin.GetStyle("Label")) {
                        alignment = TextAnchor.UpperCenter
                    };
                    GUILayout.Label("• • • • • • ".Lerp(Time.time % 1f), style, new GUILayoutOption[0]);
                }
                else
                {
                    this.OnDialogGUI();
                }
            }, string.Empty, "Box", options);
        }
    }

    protected virtual void OnInteract()
    {
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    private void UNetVersion()
    {
    }

    public Character target { get; private set; }
}

